package com.xm.adv.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xm.adv.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import xinya.com.baselibrary.constant.Constant;
import xinya.com.baselibrary.utils.ACacheX;


public class RoateDialog extends Dialog {

    private Context context;
    private ViewHolder viewHolder;
    int screen1 = 0;
    int screen2 = 0;

    public interface OnScreenChangeListener {
        void change(int screen1,int screen2);
    }

    OnScreenChangeListener onScreenChangeListener;

    public RoateDialog(Context context, OnScreenChangeListener onScreenChangeListener) {
        super(context, R.style.dialog);
        this.context = context;
        this.onScreenChangeListener = onScreenChangeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_roate, null);
        setContentView(view);
        viewHolder = new ViewHolder(view);
        String screenRoate01 = ACacheX.getAsString(context, Constant.AcacheKey.setInfo, Constant.AcacheValue.screenRoate01);
        if (TextUtils.isEmpty(screenRoate01)) {
            viewHolder.radio0001.setChecked(true);
        } else if (TextUtils.equals(screenRoate01, "0")) {
            viewHolder.radio0001.setChecked(true);
        } else if (TextUtils.equals(screenRoate01, "90")) {
            viewHolder.radio0002.setChecked(true);
        } else if (TextUtils.equals(screenRoate01, "180")) {
            viewHolder.radio0003.setChecked(true);
        } else if (TextUtils.equals(screenRoate01, "270")) {
            viewHolder.radio0004.setChecked(true);
        }
        String screenRoate02 = ACacheX.getAsString(context, Constant.AcacheKey.setInfo, Constant.AcacheValue.screenRoate02);
        if (TextUtils.isEmpty(screenRoate02)) {
            viewHolder.radio0101.setChecked(true);
        } else if (TextUtils.equals(screenRoate02, "0")) {
            viewHolder.radio0101.setChecked(true);
        } else if (TextUtils.equals(screenRoate02, "90")) {
            viewHolder.radio0102.setChecked(true);
        } else if (TextUtils.equals(screenRoate02, "180")) {
            viewHolder.radio0103.setChecked(true);
        } else if (TextUtils.equals(screenRoate02, "270")) {
            viewHolder.radio0104.setChecked(true);
        }
        viewHolder.radioGroup01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio0001:
                        screen1 = 0;
                        break;
                    case R.id.radio0002:
                        screen1 = 90;
                        break;
                    case R.id.radio0003:
                        screen1 = 180;
                        break;
                    case R.id.radio0004:
                        screen1 = 270;
                        break;

                }
            }
        });
        viewHolder.radioGroup02.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio0101:
                        screen2 = 0;
                        break;
                    case R.id.radio0102:
                        screen2 = 90;
                        break;
                    case R.id.radio0103:
                        screen2 = 180;
                        break;
                    case R.id.radio0104:
                        screen2 = 270;
                        break;
                }
            }
        });

        viewHolder.textQueDing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onScreenChangeListener.change(screen1,screen2);
                ACacheX.putAsString(context, Constant.AcacheKey.setInfo,Constant.AcacheValue.screenRoate01,String.valueOf(screen1));
                ACacheX.putAsString(context, Constant.AcacheKey.setInfo,Constant.AcacheValue.screenRoate02,String.valueOf(screen2));
            }
        });
        viewHolder.textQuXiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }


    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (event.getButtonState() == 128) {
            dismiss();
        }
        return super.onGenericMotionEvent(event);
    }

    static
    class ViewHolder {
        @BindView(R.id.textTitle)
        TextView textTitle;
        @BindView(R.id.radio0001)
        RadioButton radio0001;
        @BindView(R.id.radio0002)
        RadioButton radio0002;
        @BindView(R.id.radio0003)
        RadioButton radio0003;
        @BindView(R.id.radio0004)
        RadioButton radio0004;
        @BindView(R.id.radioGroup01)
        RadioGroup radioGroup01;
        @BindView(R.id.radio0101)
        RadioButton radio0101;
        @BindView(R.id.radio0102)
        RadioButton radio0102;
        @BindView(R.id.radio0103)
        RadioButton radio0103;
        @BindView(R.id.radio0104)
        RadioButton radio0104;
        @BindView(R.id.radioGroup02)
        RadioGroup radioGroup02;
        @BindView(R.id.textQuXiao)
        Button textQuXiao;
        @BindView(R.id.textQueDing)
        Button textQueDing;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}